import { defineStore } from "pinia";
import { ref } from "vue";
import { reqLogin } from "../../api/user";

export const useUserStore = defineStore("user", () => {
  const token = ref(uni.getStorageSync("token") || "");
  // 登录
  const login = async (code: string) => {
    const res = await reqLogin(code);
    token.value = res.token;
    // 存储Token
    uni.setStorageSync("token", res.token);
  };
  // 退出登录，清空token
  const logout = async () => {
    token.value = "";
    uni.removeStorageSync("token");
    uni.navigateTo({
      url: "/pages/login/index",
    });
  };

  return {
    token,
    login,
    logout,
  };
});
